﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyKho.PhongMau
{
    public partial class PhongMau_KhongDuyetPopup : Form
    {
        public static string MaID = "0";
        public bool update = false;

        public PhongMau_KhongDuyetPopup()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
        }

        public void Loaddata()
        {
            if (MaID == "0")
            {
                LoadGrid();
                SetPropertiesColumns();
            }
            else
            {
                SetPropertiesColumns();
                var datank = DataAll.SelectAll("PhongMau_KhongDuyet", "*", "WHERE MaIDNhapKho =" + MaID);
                luKhochuyen.EditValue = datank.Rows[0]["MaKhoChuyen"].ToString();
                txtTenkhochuyen.Text = datank.Rows[0]["MaKhoChuyen"].ToString();
                luKhonhan.EditValue = datank.Rows[0]["MaKhoNhan"].ToString();
                txtTenkhonhan.Text = datank.Rows[0]["MaKhoNhan"].ToString();
                luNguoiGiao.EditValue = datank.Rows[0]["NguoiGiao"].ToString();
                luNguoiNhan.EditValue = datank.Rows[0]["NguoiNhan"].ToString();
                txtDienGiai.Text = datank.Rows[0]["DienGiai"].ToString();
                txtChungtu.Text = datank.Rows[0]["SoChungTu"].ToString();
                //luMaDangAnh.EditValue = datank.Rows[0]["MaAnh"].ToString();
                var data = DataAll.SelectAll("Kho_HangTrongKho htk LEFT JOIN PhongMau_SanPham sp " +
                                             "ON htk.IdSanPham = sp.IdSanPham",
                                             "htk.[IDHangTrongKho],htk.[MaIDNhapKho],htk.[IdSanPham]" +
                                             ",htk.[SoLuong] as 'Số lượng',htk.[DonGia] as 'Đơn giá'" +
                                             ",htk.[ThanhTien] as 'Thành tiền',htk.[DuDinhSX] as 'Dự định SX'" +
                                             ",htk.[SoLo] as 'Số lô',htk.[GhiChu] as 'Ghi chú',sp.TenHang as 'Mã hàng'" +
                                             ",sp.TenHang as 'Diễn giải'",
                                             " WHERE MaIDNhapKho =" + MaID);
                gridNhapkho.DataSource = null;
                gridNhapkho.DataSource = data;
                //bntLuLai.Visible = false;
                //gbThongTinChung.Enabled = false;
                //gvNhapkho.OptionsBehavior.Editable = false;
                LoadImage();
                (gridNhapkho.MainView as GridView).AddNewRow();
                gvNhapkho.FocusedRowHandle = data.Rows.Count;
            }
            luNguoiGiao.Properties.DataSource = DataAll.SelectAll("DkChung_NhanVien", "MaNhanVien,TenNhanVien", "");
            luNguoiGiao.Properties.Columns.Add(new LookUpColumnInfo("MaNhanVien"));
            luNguoiGiao.Properties.Columns.Add(new LookUpColumnInfo("TenNhanVien"));
            luNguoiGiao.Properties.DisplayMember = "TenNhanVien";
            luNguoiGiao.Properties.ValueMember = "MaNhanVien";
            luNguoiGiao.Properties.Columns["MaNhanVien"].Visible = false;
            luNguoiGiao.Properties.NullText = string.Empty;
            luNguoiGiao.Properties.TextEditStyle = TextEditStyles.Standard;
            //luNguoiGiao.Properties.SearchMode = SearchMode.AutoComplete;
            luNguoiGiao.Properties.ShowHeader = false;

            luNguoiNhan.Properties.DataSource = DataAll.SelectAll("DkChung_NhanVien", "MaNhanVien,TenNhanVien", "");
            luNguoiNhan.Properties.Columns.Add(new LookUpColumnInfo("MaNhanVien"));
            luNguoiNhan.Properties.Columns.Add(new LookUpColumnInfo("TenNhanVien"));
            luNguoiNhan.Properties.DisplayMember = "TenNhanVien";
            luNguoiNhan.Properties.ValueMember = "MaNhanVien";
            luNguoiNhan.Properties.Columns["MaNhanVien"].Visible = false;
            luNguoiNhan.Properties.NullText = string.Empty;
            luNguoiNhan.Properties.TextEditStyle = TextEditStyles.Standard;
            //luNguoiNhan.Properties.SearchMode = SearchMode.AutoComplete;
            luNguoiNhan.Properties.ShowHeader = false;

            luMaDangAnh.Properties.DataSource = DataAll.SelectAll("DkChung_HinhAnh", "MaAnh", "");
            luMaDangAnh.Properties.Columns.Add(new LookUpColumnInfo("MaAnh"));
            //luMaDangAnh.Properties.Columns.Add(new LookUpColumnInfo("IDHinhAnh"));
            luMaDangAnh.Properties.DisplayMember = "MaAnh";
            luMaDangAnh.Properties.ValueMember = "MaAnh";
            //luMaDangAnh.Properties.Columns["MaAnh"].Visible = false;
            luMaDangAnh.Properties.NullText = string.Empty;
            luMaDangAnh.Properties.TextEditStyle = TextEditStyles.Standard;
            luMaDangAnh.Properties.ShowHeader = false;

        }

        public void LoadGrid()
        {
            var dataColumns = new DataTable();
            dataColumns.Columns.Add("Mã hàng");
            dataColumns.Columns.Add("Diễn giải");
            dataColumns.Columns.Add("Kho");
            dataColumns.Columns.Add("Số lượng", typeof(decimal));
            dataColumns.Columns.Add("Đơn giá", typeof(decimal));
            dataColumns.Columns.Add("Thành tiền", typeof(decimal));
            dataColumns.Columns.Add("Dự định SX", typeof(DateTime));
            dataColumns.Columns.Add("Số lô");
            dataColumns.Columns.Add("Ghi chú");
            dataColumns.Columns.Add("TK Nợ");
            dataColumns.Columns.Add("TK Có");
            pbAnh.Image = null;
            for (int i = 0; i < 1; i++)
            {
                dataColumns.Rows.Add("");
            }
            gridNhapkho.DataSource = null;
            gridNhapkho.DataSource = dataColumns;
        }

        public void SetPropertiesGrid()
        {
            gvNhapkho.OptionsView.ShowGroupPanel = false;
            gvNhapkho.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            gvNhapkho.OptionsView.ShowFooter = true;
            for (int i = 0; i < 8; i++)
            {
                gvNhapkho.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
            }

            //Total columns
            gcMaHang.Summary.Add(DevExpress.Data.SummaryItemType.Count, "Mã hàng", "{0:0,0}");
            gcDienGiai.Summary.Add(DevExpress.Data.SummaryItemType.Count, "Diễn giải", "{0:0,0}");
            gcKho.Summary.Add(DevExpress.Data.SummaryItemType.Count, "Kho", "{0:0,0}");
            gcSoLuong.Summary.Add(DevExpress.Data.SummaryItemType.Sum, "Số lượng", "{0:0,0}");
            gcDonGia.Summary.Add(DevExpress.Data.SummaryItemType.Sum, "Đơn giá", "{0:0,0}");
            gcThanhTien.Summary.Add(DevExpress.Data.SummaryItemType.Sum, "Thành tiền", "{0:0,0}");
            gcDuDinhSX.Summary.Add(DevExpress.Data.SummaryItemType.Count, "Dự định SX", "{0:0,0}");
            gcSoLo.Summary.Add(DevExpress.Data.SummaryItemType.Count, "Số lô", "{0:0,0}");
        }

        public void SetPropertiesColumns()
        {
            //Ma hang
            luMaHang.DataSource = DataAll.SelectAll("PhongMau_SanPham", "IdSanPham,MaHang,TenHang,SoLuong", "");
            luMaHang.Columns.Add(new LookUpColumnInfo("IdSanPham"));
            luMaHang.Columns.Add(new LookUpColumnInfo("MaHang"));
            luMaHang.Columns.Add(new LookUpColumnInfo("TenHang"));
            luMaHang.Columns.Add(new LookUpColumnInfo("SoLuong"));
            luMaHang.DisplayMember = "MaHang";
            luMaHang.ValueMember = "TenHang";
            luMaHang.NullText = string.Empty;
            luMaHang.Columns["IdSanPham"].Visible = false;

            //delete
            gcXoa.Width = 20;
            gcXoa.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //kho
            var data = DataAll.SelectAll("DkChung_Kho", "MaKho,TenKho", "");
            luKho.DataSource = data;
            luKho.DisplayMember = "MaKho";
            luKho.ValueMember = "MaKho";
            luKho.NullText = string.Empty;
            //
            luKhochuyen.Properties.DataSource = data;
            luKhochuyen.Properties.DisplayMember = "MaKho";
            luKhochuyen.Properties.ValueMember = "MaKho";
            //
            luKhonhan.Properties.DataSource = data;
            luKhonhan.Properties.DisplayMember = "MaKho";
            luKhonhan.Properties.ValueMember = "MaKho";

            //TK No
            DataTable tkno = new DataTable();
            tkno.Columns.Add("Số TK");
            tkno.Columns.Add("Tên TK");
            tkno.Rows.Add("147", "Nguyên liệu, vật liệu");
            tkno.Rows.Add("148", "Công cụ, dụng cụ");
            tkno.Rows.Add("149", "Thành phẩm");
            tkno.Rows.Add("150", "Giá mua hàng hóa");
            tkno.Rows.Add("151", "Chi phí thu mua hàng hóa");
            luTKNo.DataSource = tkno;
            luTKNo.DisplayMember = "Số TK";
            luTKNo.ValueMember = "Số TK";
            luTKNo.NullText = string.Empty;

            //TK co
            DataTable tkco = new DataTable();
            tkco.Columns.Add("Số TK");
            tkco.Columns.Add("Tên TK");
            tkco.Rows.Add("147", "Tài sản thuê ngoài");
            tkco.Rows.Add("327", "Vật tư hàng hóa nhận giữ hộ");
            tkco.Rows.Add("235", "Hàng hóa nhận bán hộ");
            tkco.Rows.Add("726", "Ngoại tệ các loại");
            tkco.Rows.Add("278", "Tiền việt");
            luTKCo.DataSource = tkco;
            luTKCo.DisplayMember = "Số TK";
            luTKCo.ValueMember = "Số TK";
            luTKCo.NullText = string.Empty;

            //Format columns
            gcSoLuong.DisplayFormat.FormatType = FormatType.Numeric;
            gcSoLuong.DisplayFormat.FormatString = "{0:0,0}";
            gcDonGia.DisplayFormat.FormatType = FormatType.Numeric;
            gcDonGia.DisplayFormat.FormatString = "{0:0,0}";
            gcThanhTien.DisplayFormat.FormatType = FormatType.Numeric;
            gcThanhTien.DisplayFormat.FormatString = "{0:0,0}";
            gcDuDinhSX.DisplayFormat.FormatType = FormatType.Custom;
            gcDuDinhSX.DisplayFormat.FormatString = "dd/MM/yyyy";
            gcThanhTien.OptionsColumn.AllowEdit = false;

            //Alig Columns
            gcMaHang.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcDienGiai.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcKho.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLuong.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            gcDonGia.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcDonGia.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            gcThanhTien.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcThanhTien.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Far;
            gcDuDinhSX.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSoLo.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Chung tu
            if (!update)
            {
                txtChungtu.Text = "NK" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Year
                + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
            }
            //visible
            gcTKNo.Visible = false;
            gcTKCo.Visible = false;
            gcKho.Visible = false;
        }

        public bool SoLuong(int slkho, int sllay, string mahang)
        {
            int soluong = slkho - sllay;
            if (soluong > 0)
            {
                DataAll.Update("PhongMau_SanPham", "SoLuong=" + soluong + " WHERE MaHang='" + mahang + "'");
                return false;
            }
            return true;
        }

        private void gvNhapkho_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.Caption == "Mã hàng")
            {
                gvNhapkho.SetRowCellValue(e.RowHandle, gvNhapkho.Columns["Diễn giải"], e.Value);
                gvNhapkho.SetRowCellValue(e.RowHandle, gvNhapkho.Columns["Số lượng"], "0");
                gvNhapkho.SetRowCellValue(e.RowHandle, gvNhapkho.Columns["Đơn giá"], "0");
                gvNhapkho.SetRowCellValue(e.RowHandle, gvNhapkho.Columns["Thành tiền"], "0");
            }
            if (e.Column.Caption == "Số lượng" && !string.IsNullOrEmpty(gvNhapkho.GetRowCellValue(e.RowHandle, gvNhapkho.Columns["Số lượng"]).ToString())
            && !string.IsNullOrEmpty(gvNhapkho.GetRowCellValue(e.RowHandle, gvNhapkho.Columns["Đơn giá"]).ToString()))
            {
                gvNhapkho.SetRowCellValue(e.RowHandle, gvNhapkho.Columns["Thành tiền"], Convert.ToDecimal(gvNhapkho.GetRowCellValue(e.RowHandle, "Số lượng"))
                    * Convert.ToDecimal(gvNhapkho.GetRowCellValue(e.RowHandle, "Đơn giá")));
                //var mahang = (DataRowView)luMaHang.GetDataSourceRowByKeyValue(gcMaHang.View.GetRowCellValue(e.RowHandle, "Mã hàng").ToString());
                //if (SoLuong(Convert.ToInt32(mahang.Row.ItemArray[3]), Convert.ToInt32(gcSoLuong.View.GetRowCellValue(e.RowHandle, "Số lượng")), mahang.Row.ItemArray[1].ToString()))
                //{
                //    MessageBox.Show("Số lượng trong kho không đủ", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    gcSoLuong.View.SetRowCellValue(0, "Số lượng", 0);
                //    //gcSoLuong.View.SetColumnError(null, "sdfsdf");
                //}
                //gvNhapkho.GetRowCellValue(e.RowHandle, gvNhapkho.Columns["Số lượng"])
            }
            if (e.Column.Caption == "Đơn giá" && !string.IsNullOrEmpty(gvNhapkho.GetRowCellValue(e.RowHandle, gvNhapkho.Columns["Số lượng"]).ToString())
            && !string.IsNullOrEmpty(gvNhapkho.GetRowCellValue(e.RowHandle, gvNhapkho.Columns["Đơn giá"]).ToString()))
            {
                gvNhapkho.SetRowCellValue(e.RowHandle, gvNhapkho.Columns["Thành tiền"], Convert.ToDecimal(gvNhapkho.GetRowCellValue(e.RowHandle, "Số lượng"))
                    * Convert.ToDecimal(gvNhapkho.GetRowCellValue(e.RowHandle, "Đơn giá")));
            }
            if (e.RowHandle == gvNhapkho.RowCount - 1)
            {
                (gridNhapkho.MainView as GridView).AddNewRow();
            }
        }

        private void gvNhapkho_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {

        }

        private void repositoryItemLookUpEdit1_ProcessNewValue(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
        {
            //DialogResult oDlgResult = XtraMessageBox.Show("Ban phai chon 1 truong", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            //if (oDlgResult == System.Windows.Forms.DialogResult.OK)
            //{
            //    data.Rows.Add(e.DisplayValue.ToString());
            //    data.AcceptChanges();

            //    if (gridView1 != null)
            //        gridView1.SetFocusedRowCellValue("Name", e.DisplayValue);
            //}
            //repositoryItemLookUpEdit1.ProcessNewValue += repositoryItemLookUpEdit1_ProcessNewValue;
        }

        private void luKhochuyen_EditValueChanged(object sender, EventArgs e)
        {
            if (luKhochuyen.ItemIndex >= 0)
            {
                txtTenkhochuyen.Text = luKhochuyen.Properties.GetDataSourceValue("TenKho", luKhochuyen.ItemIndex).ToString();
            }
        }

        private void lookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (luKhonhan.ItemIndex >= 0)
            {
                txtTenkhonhan.Text = luKhonhan.Properties.GetDataSourceValue("TenKho", luKhonhan.ItemIndex).ToString();
            }
        }

        private void bntThemMoi_Click(object sender, EventArgs e)
        {
            //(gridNhapkho.MainView as GridView).AddNewRow();
        }

        private void bntLuLai_Click(object sender, EventArgs e)
        {
            if (update)
            {
                string message = string.Empty;
                message = DataAll.Update("PhongMau_KhongDuyet", "MaKhoChuyen='" + luKhochuyen.EditValue
                + "',MaKhoNhan='" + luKhonhan.EditValue + "',NguoiGiao='" + luNguoiGiao.EditValue
                + "', NguoiNhan='" + luNguoiNhan.EditValue + "', NgayChungTu='" + dtNgayChungTu.Value
                + "', NgayHoachToan='" + dtNgayHachToan.Value + "', SoChungTu='" + txtChungtu.Text
                + "', DienGiai='" + txtDienGiai.Text + "' WHERE MaIDNhapKho=" + MaID);
                if (message == "success")
                {
                    var dataview = (DataView)gvNhapkho.DataSource;
                    for (int i = 0; i < gvNhapkho.RowCount - 1; i++)
                    {
                        var mahang = (DataRowView)luMaHang.GetDataSourceRowByKeyValue(gcMaHang.View.GetRowCellValue(i, "Mã hàng").ToString());
                        if (string.IsNullOrEmpty(gcSoLuong.View.GetRowCellValue(i, "IDHangTrongKho").ToString()))
                        {
                            message = DataAll.InsertAll("Kho_HangTrongKho", MaID + "," + mahang.Row.ItemArray[0]
                                + "," + gcSoLuong.View.GetRowCellValue(i, "Số lượng")
                                + "," + gcDonGia.View.GetRowCellValue(i, "Đơn giá")
                                + "," + gcThanhTien.View.GetRowCellValue(i, "Thành tiền")
                                + "," + gcDuDinhSX.View.GetRowCellValue(i, "Dự định SX")
                                + "," + gcSoLo.View.GetRowCellValue(i, "Số lô")
                                + "," + gcSoLo.View.GetRowCellValue(i, "Ghi chú"));
                            if (message == "success")
                            {
                                message = DataAll.Update("PhongMau_SanPham", " TrangThai = " + Convert.ToInt32(TrangThai.PhongMau_TaoMoi) + " WHERE MaHang='" + mahang.Row.ItemArray[1] + "'");
                            }
                            if (message != "success")
                            {
                                DataAll.Delete("PhongMau_KhongDuyet", "WHERE MaIDNhapKho=" + MaID);
                                break;
                            }
                        }
                        else
                        {
                            message = DataAll.Update("Kho_HangTrongKho", "IdSanPham=" + mahang.Row.ItemArray[0]
                            + ",SoLuong=" + gcSoLuong.View.GetRowCellValue(i, "Số lượng")
                            + ",DonGia=" + gcDonGia.View.GetRowCellValue(i, "Đơn giá")
                            + ",ThanhTien=" + gcThanhTien.View.GetRowCellValue(i, "Thành tiền")
                            + ",DuDinhSX='" + gcDuDinhSX.View.GetRowCellValue(i, "Dự định SX")
                            + "',SoLo='" + gcSoLo.View.GetRowCellValue(i, "Số lô")
                            + "',GhiChu='" + gcSoLo.View.GetRowCellValue(i, "Ghi chú")
                            + "' WHERE IDHangTrongKho=" + dataview[i]["IDHangTrongKho"]);
                        }
                        if (message != "success")
                        {
                            break;
                        }
                    }
                }
                MessageBox.Show(message.Equals("success") ? "Sửa nhập kho thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
            }
            else
            {
                if (gvNhapkho.RowCount > 1)
                {
                    string message = string.Empty;
                    var data = DataAll.SelectCustomer("INSERT INTO [PhongMau_KhongDuyet] VALUES(0,'" + luKhochuyen.EditValue + "','" + luKhonhan.EditValue
                        + "','" + luNguoiGiao.EditValue + "','" + luNguoiNhan.EditValue
                        + "','" + dtNgayChungTu.Value.ToString("MM/dd/yyyy") + "','" + dtNgayHachToan.Value.ToString("MM/dd/yyyy") + "','" + txtChungtu.Text
                        + "','" + txtDienGiai.Text + "'," + Convert.ToInt32(TrangThai.PhongMau_TaoMoi) + ") SELECT MAX(MaIDNhapKho) FROM PhongMau_KhongDuyet");
                    for (int i = 0; i < gvNhapkho.RowCount - 1; i++)
                    {
                        var mahang = (DataRowView)luMaHang.GetDataSourceRowByKeyValue(gcMaHang.View.GetRowCellValue(i, "Mã hàng").ToString());
                        message = DataAll.InsertAll("Kho_HangTrongKho", data.Rows[0][0] + "," + mahang.Row.ItemArray[0]
                            + "," + gcSoLuong.View.GetRowCellValue(i, "Số lượng")
                            + "," + gcDonGia.View.GetRowCellValue(i, "Đơn giá")
                            + "," + gcThanhTien.View.GetRowCellValue(i, "Thành tiền")
                            + "," + gcDuDinhSX.View.GetRowCellValue(i, "Dự định SX")
                            + "," + gcSoLo.View.GetRowCellValue(i, "Số lô")
                            + "," + gcSoLo.View.GetRowCellValue(i, "Ghi chú"));
                        if (message == "success")
                        {
                            message = DataAll.Update("PhongMau_SanPham", " TrangThai = " + Convert.ToInt32(TrangThai.PhongMau_TaoMoi) + " WHERE MaHang='" + mahang.Row.ItemArray[1] + "'");
                        }
                        if (message != "success")
                        {
                            DataAll.Delete("PhongMau_KhongDuyet", "WHERE MaIDNhapKho=" + data.Rows[0][0]);
                            break;
                        }
                    }
                    MessageBox.Show(message.Equals("success") ? "Thêm nhập kho thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    LoadGrid();
                }
            }
        }

        private void textBox9_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                LoadImage();
            }
        }

        public void LoadImage()
        {
            var data = DataAll.SelectAll("DkChung_HinhAnh", "*", " WHERE MaAnh='" + luMaDangAnh.EditValue + "'");
            if (data.Rows.Count > 0)
            {
                var content = (byte[])data.Rows[0]["Anh"];
                if (content != null)
                {
                    Image newImage;
                    using (var ms = new MemoryStream(content, 0, content.Length))
                    {
                        ms.Write(content, 0, content.Length);
                        newImage = Image.FromStream(ms, true);
                    }
                    pbAnh.Image = newImage;
                }
                else
                {
                    pbAnh.Image = null;
                }
            }
            else
            {
                MessageBox.Show("Không tìm thấy mã ảnh", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void luMaDangAnh_EditValueChanged(object sender, EventArgs e)
        {
            var lucheck = (LookUpEdit)sender;
            if (lucheck.ItemIndex >= 0)
            {
                LoadImage();
            }
        }

        private void buttonDelete_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                var dataview = (DataView)gvNhapkho.DataSource;
                if (MaID != "0")
                {
                    if (!string.IsNullOrEmpty(gvNhapkho.GetRowCellValue(gvNhapkho.FocusedRowHandle, "IDHangTrongKho").ToString()))
                    {

                        string message = DataAll.Delete("Kho_HangTrongKho", " IDHangTrongKho = " + dataview[gvNhapkho.FocusedRowHandle]["IDHangTrongKho"]);
                        MessageBox.Show(message.Equals("success") ? "Xóa hàng trong kho thành công" : message, "Message",
                                            MessageBoxButtons.OK,
                                            message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    }
                }
                (gridNhapkho.MainView as GridView).DeleteSelectedRows();
            }
        }

    }
}
